package com.csglxt.view.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.csglxt.base.BaseAction;
import com.csglxt.domain.Supplier;
import com.csglxt.utils.QueryHelper;

/**
 * 
 * @ClassName: SupplierAction
 * @Description: 供应商控制器
 * @author Delver_Si
 * @date 2015-5-28 下午11:59:53
 * 
 */

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class SupplierAction extends BaseAction<Supplier> {

    private String Searchname;

    public String getSearchname() {
        return Searchname;
    }

    public void setSearchname(String searchname) {
        Searchname = searchname;
    }

    /**
     * 供应商列表
     * 
     * @return
     */
    public String list() {
        // List<Supplier> supplierList = supplierService.findAll();
        // ActionContext.getContext().put("supplierList", supplierList);

        new QueryHelper(Supplier.class, "s").addOrderProperty("name", true)
                .preparePageBean(supplierService, pageNum, pageSize);
        return "list";
    }

    /**
     * 供应商列表模糊查询
     * 
     * @return
     */
    public String Searchlist() {
        QueryHelper qureyHelper = new QueryHelper(Supplier.class, "s")
                .addOrderProperty("name", true);
        if (Searchname != null && !Searchname.isEmpty()) {
            qureyHelper.addCondition(" s.name like ? ", "%" + Searchname + "%");

        }

        qureyHelper.preparePageBean(supplierService, pageNum, pageSize);
        return "list";
    }

    /**
     * 修改页面
     * 
     * @return
     */
    public String editUI() {
        getValueStack().push(supplierService.getById(model.getId()));
        return "saveUI";
    }

    /**
     * 保存供应商资料
     * 
     * @return
     */

    public String save() {
        supplierService.update(model);
        return "toList";
    }

    /**
     * 添加页面
     * 
     * @return
     */
    public String addUI() {
        return "saveUI";
    }

    /**
     * 添加供应商
     * 
     * @return
     */
    public String add() {
        supplierService.save(model);
        return "toList";
    }

    /**
     * 解除合约
     * 
     * @return
     */
    public String delete() {
        supplierService.delete(model.getId());
        return "toList";
    }

    /**
     * 
     * @Title: ajx
     * @Description: TODO(对页面的商品名称进行验证)
     * @param
     * @return json格式 success为true表示已存在，false表示不存在
     * @throws
     */
    public void ajx() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();

        response.setContentType("text/html;charset=utf-8");
        // 处理get请求乱码
        model.setName(new String(model.getName().getBytes("ISO-8859-1"),
                "UTF-8"));
        Supplier supplier = supplierService.findSupplierByName(model);

        if (supplier != null) {
            // 重点用是用print输出的数据格式是json

            response.getWriter().print("{\"success\":true,\"msg\":\"已存在\"}");
        } else {

            response.getWriter().print("{\"success\":false,\"msg\":\"\"}");
        }
    }
}
